package cn.marinda.spritefragment.data;

import cn.marinda.spritefragment.SpriteFragment;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.*;

public class ItemsData implements DataObject,DataInterface{
    private YamlConfiguration item = null;
    private ConfigurationSection section = null;
    private Map<String,String> TestMaterials = null;
    private Set<String> keys;
    //重命名
    private Map<String,String> displayName;
    //lore
    private Map<String, List<String>> lore;


    private Map<String,String> itemMaterial = null;
    /*
    CraftItems Config List
    */
    //craftItems
    private ConfigurationSection craft = null;
    //material
    private Map<String, String> Materials = null;
    //CraftItems DisplayNames <键名，DisplayName>
    private Map<String,String> craftDisplayNames = null;
    //CraftItems Lore <键名,List<String>>
    private Map<String,List<String>> craftLores = null;
    //CraftItems Number <键名,Integer>
    private Map<String,Integer> craftNumbers = null;

    //获取当前所有键
    private Set<String>  itemKeys = null;
    private ItemStack items = null;
    //
    private Map<String, ItemStack> itemsItemStack = null;

    private ItemMeta itemMeta;
    public ItemsData(){
        defaultConfig();
        defaultGetConfig();
    }

    @Override
    public YamlConfiguration getConfig() {
        return item;
    }

    @Override
    public void defaultConfig() {
        item = new YamlConfiguration();
        try {
            item.load(new File(SpriteFragment.plugin.getDataFolder(),"items.yml"));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void defaultGetConfig() {
        keys = new HashSet<>();
        displayName = new HashMap<>();
        lore = new HashMap<>();
        Materials = new HashMap<>();
        craftDisplayNames = new HashMap<>();
        craftLores = new HashMap<>();
        craftNumbers = new HashMap<>();
        TestMaterials = new HashMap<>();
        itemKeys = new HashSet<>();
        itemsItemStack = new HashMap<>();
        itemMaterial = new HashMap<>();
        defaultKey();
        defaultCraftItems();
    }
    /*
     物品信息初始化
     */
    public void defaultKey(){
        section = item.getConfigurationSection("item");
        keys = section.getKeys(false);
        for(String keyName : keys){
            itemMaterialsPut(keyName,getConfig().getString(section.getName() + "." + keyName+".material"));
            itemKeyPut(keyName);
            displayNamePut(keyName,getConfig().getString(section.getName() + "." + keyName+".displayName"));
            lorePut(keyName,getConfig().getStringList(section.getName() + "." + keyName+".lore"));
            TestMaterialsPut(keyName,getConfig().getString(section.getName() + "." + keyName + ".material"));
        }
    }

    private void defaultCraftItems(){
        craft = getConfig().getConfigurationSection("craftItems");
        itemKeys = craft.getKeys(false);
        for(String keyName : itemKeys){
            MaterialsPut(keyName,getConfig().getString(craft.getName() + "." + keyName + ".material"));
            craftDisplaysPut(keyName,getConfig().getString(craft.getName() + "." + keyName + ".displayName"));
            craftLorePut(keyName,getConfig().getStringList(craft.getName() + "." + keyName+".lore"));
            craftNumberPut(keyName,getConfig().getInt(craft.getName() + "." + keyName+".number"));
        }
    }

    public void displayNamePut(String keyName,String displayNames){
        if(!displayName.containsKey(keyName)){
           displayName.put(keyName,displayNames);
        }
    }

    public void lorePut(String keyName,List<String> lores){
        if(!lore.containsKey(keyName)){
            lore.put(keyName,lores);
        }
    }
    public void MaterialsPut(String keyName,String item){
        if(!Materials.containsKey(keyName)){
            Materials.put(keyName,item);
        }
    }

    public void itemMaterialsPut(String keyName,String item){
        if(!itemMaterial.containsKey(keyName)){
            itemMaterial.put(keyName,item);
        }
    }
    public void craftDisplaysPut(String keyName,String displayNames){
        if(!craftDisplayNames.containsKey(keyName)){
            craftDisplayNames.put(keyName,displayNames);
        }
    }
    public void TestMaterialsPut(String keyName,String item){
        if(!TestMaterials.containsKey(keyName)){
            TestMaterials.put(keyName,item);
        }
    }
    public void craftLorePut(String keyName,List<String> lores){
        if(!craftLores.containsKey(keyName)){
            craftLores.put(keyName,lores);
        }
    }
    public void craftNumberPut(String keyName,int number){
        if(!craftNumbers.containsKey(keyName)){
            craftNumbers.put(keyName,number);
        }
    }
    public void itemKeyPut(String keyName){
        if(!itemKeys.contains(keyName)){
            itemKeys.add(keyName);
        }
    }
    public Map<String, String> getDisplayName() {
        return displayName;
    }

    public Map<String, List<String>> getLore() {
        return lore;
    }

    public Map<String, String> getCraftDisplayNames() {
        return craftDisplayNames;
    }

    public Map<String, List<String>> getCraftLores() {
        return craftLores;
    }

    public Map<String, Integer> getCraftNumbers() {
        return craftNumbers;
    }

    public Map<String, String> getMaterials() {
        return Materials;
    }

    public Set<String> getItemKeys() {
        return itemKeys;
    }

    @Override
    public Set<String> getKeys() {
        return keys;
    }

    @Override
    public ItemStack getResultItems(String key) {
        if(craft.getKeys(false).contains(key)){
            items = new ItemStack(Material.matchMaterial(getMaterials().get(key)));
            itemMeta = items.getItemMeta();
            itemMeta.setDisplayName(getCraftDisplayNames().get(key));
            itemMeta.setLore(getCraftLores().get(key));
            items.setItemMeta(itemMeta);
            return items;
        }
        return null;
    }

    @Override
    public ItemStack getItems(String key) {
        if(keys.contains(key)){
            Material ss = Material.matchMaterial(getItemMaterial().get(key));
            items = new ItemStack(Material.matchMaterial(getItemMaterial().get(key)));
            itemMeta = items.getItemMeta();
            itemMeta.setDisplayName(getDisplayName().get(key));
            itemMeta.setLore(getLore().get(key));
            items.setItemMeta(itemMeta);
            return items;
        }
        return null;
    }

    @Override
    public int getItemPayNumber(String key) {
        return -1;
    }

    @Override
    public String getKeyMark(int index,String parrentKey) {
        return null;
    }

    @Override
    public int getNumber(int index, String parrentKey) {
        return 0;
    }

    @Override
    public String getExchangeResule(String key) {
        return null;
    }

    public Map<String, String> getItemMaterial() {
        return itemMaterial;
    }

    public ItemMeta getItemMeta() {
        return itemMeta;
    }
}
